<?php
 /*
   * Author : 1. Novi Setiani
   *          2.
   * Last edited : 4 Mei 2011
 */

    class Peserta extends CI_Model
    {
        function Peserta_model() {
                parent::Model();
        }

		public function add($activity_id, $user_id){
                 /*    if ($activity_type == 1)
                                    $peserta = 'peserta';
                    else
                                    $peserta = 'pesertanonrutin';
		*/
				$sql = "insert into peserta (activityID, userID) values ($activity_id, '$user_id')";
			
			return $this->db->query($sql);
		}
		
		public function delete($activity_id, $user_id){
               /*       if ($activity_type == 1)
                                    $peserta = 'peserta';
                    else
                                    $peserta = 'pesertanonrutin';
                * *
                */
			$sql = "delete from peserta where activityID=? and userID=?";
			return $this->db->query($sql, array($activity_id, $user_id));
		}
		public function getPesertaByActivity ($activityID){
                    $sql = "select name from pengguna left join peserta on peserta.userID=pengguna.userID where peserta.activityID = ?";
                    $query = $this->db->query($sql, array($activityID));
                    if ($query->num_rows() > 0){
				$result = array();
				foreach($query->result_array() as $row){
					$result[] = $row;
				}
				return $result;
			 }

                }
		public function getPeserta($activity_id, $user_id=null){
                      /*if ($activity_type == 1)
                                    $peserta = 'peserta';
                        else
                                    $peserta = 'pesertanonrutin';*/
			if($user_id){
				$sql = 'SELECT * from pengguna where userID = ?';
				$query = $this->db->query($sql, array($user_id));
				if ($query->num_rows() > 0){
					$row = $query->row_array();
					return $row;
				}
			}
			else{
				$sql = "SELECT * from pengguna where userID in (select userID from peserta where activityID  = ?)";
				$query = $this->db->query($sql, array($activity_id));
				if ($query->num_rows() > 0){
				$result = array();
				foreach($query->result_array() as $row){
					$result[] = $row;					
				}
				return $result;
			 }
			}
			
			
		}
    }
?>
